<?php

namespace app\admin\model;


/**
 * 分站模型
 * Class Menu
 * @package app\admin\model
 */
class Substation extends Base
{
    protected $autoWriteTimestamp = true;//开启自动添加时间戳
    protected $updateTime = false;
    protected $insert = ['substation_key'];//自动添加

    //添加分站秘钥唯一
    protected function setSubstationKeyAttr()
    {
        $val = md5(RandomString());
        if($this->where("substation_key",$val)->count()){
            $this->setSubstationKeyAttr();
        }
        return $val;
    }


    //分站列表
    public function substationList($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("substation_name","like","%".$keyword."%")->whereOr("substation_contacts","like","%".$keyword."%")->whereOr("substation_domain","like","%".$keyword."%");
                }
            })
            ->where(config("TableDeleteField"),0)
            ->select();
    }

    //分站列表总数
    public function substationListCount($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("substation_name","like","%".$keyword."%")->whereOr("substation_contacts","like","%".$keyword."%")->whereOr("substation_domain","like","%".$keyword."%");
                }
            })
            ->where(config("TableDeleteField"),0)
            ->count();
    }

    //用户网站列表
    public function userList(){
        $substations = [];//分站集合
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {
            //获取角色网站权限
            $AdminGroup = new AdminGroup();
            $substations = $AdminGroup->where("group_id", $this->admin["group_id"])->value("substations");//获取上级角色ID
            $substations = StrToArray($substations);//转化为数组
        }
        return $this
            ->field("substation_id,substation_name")
            ->where(config("TableDeleteField"),0)
            ->where(function ($q)use ($substations){
                if(!empty($substations) && is_array($substations)){
                    $q->where("substation_id","IN",$substations);
                }
            })
            ->select();
    }

    //设置用户网站列表
    public function setUserList($group_id){
        $substations = [];//分站集合
        //获取角色网站权限
        $AdminGroup = new AdminGroup();
        $parent_group_id = $AdminGroup->where("group_id", $group_id)->value("parent_id");//获取上级角色ID
        if($parent_group_id != config("SuperAdminGroupId")) {
            $substations = $AdminGroup->where("group_id", $parent_group_id)->value("substations");//获取上级角色ID
            $substations = StrToArray($substations);//转化为数组
        }
        return $this
            ->field("substation_id,substation_name")
            ->where(config("TableDeleteField"),0)
            ->where(function ($q)use ($substations){
                if(!empty($substations) && is_array($substations)){
                    $q->where("substation_id","IN",$substations);
                }
            })
            ->select();
    }

    //用户登录设置网站使用
    public function userLoginList($group_id){
        //获取角色网站权限
        $AdminGroup = new AdminGroup();
        $substations = $AdminGroup->where("group_id", $group_id)->value("substations");
        $substations = StrToArray($substations);//转化为数组
        return $this
            ->field("substation_id,substation_name")
            ->where(config("TableDeleteField"),0)
            ->where(function ($q)use ($substations){
                if(!empty($substations) && is_array($substations)){
                    $q->where("substation_id","IN",$substations);
                }
            })
            ->select();
    }

    //用户网站列表总数
    public function userListCount($group_id){
        $substations = [];//分站集合
        if($group_id != config("SuperAdminGroupId")) {
            //获取角色网站权限
            $AdminGroup = new AdminGroup();
            $substations = $AdminGroup->where("group_id", $group_id)->value("substations");
            $substations = StrToArray($substations);//转化为数组
        }
        return $this
            ->field("substation_id,substation_name")
            ->where(config("TableDeleteField"),0)
            ->where(function ($q)use ($substations){
                if(!empty($substations) && is_array($substations)){
                    $q->where("substation_id","IN",$substations);
                }
            })
            ->count();
    }

    //检查有没有该网站权限
    public function checkUserSubstation($substation_id){
        $is = false;//分站集合
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {
            $AdminGroup = new AdminGroup();
            //获取角色网站权限
            $substations = $AdminGroup->where("group_id", $this->admin["group_id"])->value("substations");
            if (!empty($substations)) {
                if(!empty($substation_id)) {//不是总站权限在判断
                    if (strpos($substations, "[" . $substation_id . "]") !== false) {
                        $is = true;
                    }
                }else{
                    $substationsArr = StrToArray($substations);//转化为数组
                    if(count($substationsArr) > 1){//获取数组长度
                        $is = true;
                    }
                }
            }
        }else{
            $is = true;
        }
        return $is;
    }

    //验证用户设置网站权限是否正确
    public function checkSetUserList($group_id,$substations){
        $count = 0;//分站集合
        $setUserList = $this->setUserList($group_id);
        if(!empty($substations)) {
            foreach ($substations as $val) {
                foreach ($setUserList as $s_val) {
                    if ($s_val["substation_id"] == $val) {
                        $count++;
                        break;
                    }
                }
            }
        }
        return $count == count($substations) ? true : false;
    }
}